package com.jjlin.chapter_5;

import java.util.*;
public class CD140{

    //字典树（前缀树）节点
    public static class TrieNode{
        public int path; //有多少单词共用这个节点
        public int end; //有多少个单词以这个节点结束
        public HashMap<Character, TrieNode> map;
        public TrieNode(){
            this.path = 0;
            this.end = 0;
            this.map = new HashMap<Character, TrieNode>();
        }
    }

    //前缀树
    public static class Trie{
        public TrieNode root;

        public Trie(){
            root = new TrieNode();
        }

        //插入单词
        public void insert(String word){
            if(word == null || "".equals(word))
                return;
            char[] str = word.toCharArray();
            TrieNode node = root;
            for(int i = 0; i < str.length; i++){
                if(!node.map.containsKey(str[i])){
                    node.map.put(str[i], new TrieNode());
                }
                node = node.map.get(str[i]);
                node.path++;
            }
            node.end++;
        }

        //查找单词
        public boolean search(String word){
            if(word == null || "".equals(word))
                return false;
            char[] str = word.toCharArray();
            TrieNode node = root;
            for(int i = 0; i < str.length; i++){
                if(!node.map.containsKey(str[i]))
                    return false;
                node = node.map.get(str[i]);
            }
            return node.end != 0;
        }

        //删除单词
        public void delete(String word){
            if(search(word)){
                char[] str = word.toCharArray();
                TrieNode node = root;
                for(int i = 0; i < str.length; i++){
                    if(--node.map.get(str[i]).path == 0) {
                        node.map.remove(str[i]);
                        return;
                    }
                    node = node.map.get(str[i]);
                }
                node.end--;
            }
        }

        //查找以word为前缀的单词个数
        public int prefixNumber(String word){
            if(word == null || "".equals(word))
                return 0;
            TrieNode node = root;
            char[] str = word.toCharArray();
            for(int i = 0; i < str.length; i++){
                if(!node.map.containsKey(str[i]))
                    return 0;
                node = node.map.get(str[i]);
            }
            return node.path;
        }
    }

    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        Trie trie = new Trie();
        int m = sc.nextInt();
        String[] str = new String[m];
        sc.nextLine();
        for(int i = 0; i < m; i++)
            str[i] = sc.nextLine();
        for(int i = 0; i < m; i++){
            String[] s = str[i].split(" ");
            int op = Integer.parseInt(s[0]);
            String word = s[1];
            if(op == 1)
                trie.insert(word);
            else if(op == 2)
                trie.delete(word);
            else if(op == 3)
                System.out.println(trie.search(word) ? "YES" : "NO");
            else
                System.out.println(trie.prefixNumber(word));
        }
    }
}